﻿using Keras.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Keras.Helper
{
    public static class KerasModel
    {
        /// <summary>
        /// Keras训练模型
        /// </summary>
        private static BaseModel baseModel = null;

        /// <summary>
        /// 文件路径
        /// </summary>
        private static string filePath = string.Empty;

        /// <summary>
        /// 创建神经网络
        /// </summary>
        /// <returns></returns>
        public static BaseModel GetBaseModel()
        {
            if (KerasModel.baseModel != null)
                return KerasModel.baseModel;
            //选择文件
            var openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "h5|*.h5*";
            openFileDialog.RestoreDirectory = true;
            openFileDialog.FilterIndex = 1;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
                return GetBaseModel(openFileDialog.FileName);
            else return null;
        }

        /// <summary>
        /// 创建神经网络
        /// </summary>
        /// <returns></returns>
        public static BaseModel GetBaseModel(string filePath)
        {
            if (KerasModel.filePath == filePath && KerasModel.baseModel != null)
                return KerasModel.baseModel;
            KerasModel.baseModel = Sequential.LoadModel(filePath);
            KerasModel.filePath = filePath;
            return KerasModel.baseModel;
        }
    }
}
